/**
 * 封装节点显隐事件
 * @param  {Object}  svg  画布上的组件数据
 * @param  {Object}  cell  点击对象
 * @param  {Graph}  graph  画布
 * @param  {number}  count  记录当前状态 0表示未折叠 1表示折叠
 * 注释掉的部分用于实现节点连成环 只保留当前点击的节点 其余都隐藏
 * 如不考虑当前情况 可将注释代码去除
 */
export function deal(svg, cell, graph, count) {
    let edgeArr = svg.cells.filter((item) => item.shape === "edge")  // 提取edge边
    let filterArr = edgeArr.filter((item) => item.source.cell === cell.id && item.target.cell !== cell.id)
    let targetIdArr = filterArr.map((item) => item.target.cell)  // 提取目标id
    targetIdArr = unique(targetIdArr);  // 去重
    if(cell.store.data.count === 0){
        for (let i = 0; i < targetIdArr.length; i++) {
            let nodeIdToFind = targetIdArr[i]
            let foundCell = null
            let foundTable = null
            graph.getNodes().forEach((node) => {
                if (node.id === nodeIdToFind) {
                    foundCell = node;  // 寻找目标id对应的cell
                    // let suc = graph.getSuccessors(foundCell, { distance: 1 });
                    if (foundCell.store.data.children) {
                        // foundCell.store.data.children为节点绑定的列表数组
                        for (let i = 0; i < foundCell.store.data.children.length; i++) {
                            // 遍历node 提取id对应的table
                            graph.getNodes().forEach((table) => {
                                if (table.id === foundCell.store.data.children[i]) {
                                    foundTable = table;
                                    foundTable.visible = false
                                }
                            })
                        }
                    }
                    foundCell.visible = false
                    foundCell.store.data.count = 0  // 避免有些节点被点击 count为1
                    deal(svg, foundCell, graph, foundCell.store.data.count)
                    // for (let i = 0; i < suc.length; i++) {
                    //     if (graph.getSuccessors(foundCell, { distance: 1 })[i].id !== cell.id) {
                    //         deal(svg, foundCell, graph, foundCell.store.data.count)
                    //     }
                    // }
                }
            });
        }
        cell.store.data.count = 1
    }
    else {
        for (let i = 0; i < targetIdArr.length; i++) {
            let nodeIdToFind = targetIdArr[i]
            let foundCell = null
            let foundTable = null
            graph.getNodes().forEach((node) => {
                if (node.id === nodeIdToFind) {
                    foundCell = node;  // 寻找目标id对应的cell
                    // let suc = graph.getSuccessors(foundCell, { distance: 1 });
                    if (foundCell.store.data.children){
                        // foundCell.store.data.children为节点绑定的列表数组
                        for (let i = 0;i < foundCell.store.data.children.length; i++) {
                            // 遍历node 提取id对应的table
                            graph.getNodes().forEach((table) => {
                                if (table.id === foundCell.store.data.children[i]){
                                    foundTable = table;
                                    foundTable.visible = true
                                }
                            })
                        }
                    }
                    foundCell.visible = true
                    foundCell.store.data.count = 1  // 避免有些节点被2次点击 count为0
                    deal(svg, foundCell, graph, foundCell.store.data.count)
                    // for (let i = 0; i < suc.length; i++) {
                    //     if (graph.getSuccessors(foundCell, { distance: 1 })[i].id !== cell.id) {
                    //         deal(svg, foundCell, graph, foundCell.store.data.count)
                    //     }
                    // }
                }
            });
        }
        cell.store.data.count = 0
    }
}

/**
 * 实现数组去重
 * @param {array} arr
 * @returns {array}
 */
function unique(arr){
    let res=[];
    for (let i = 0; i < arr.length; i++) {
        if(res.indexOf(arr[i]) === -1){
            res.push(arr[i]);
        }
    }
    return res;
}
